//  路由请求
let express = require('express')();
// 静态请求
let serve = require('express-static');
// 规划一个连接数据库
let mysql = require('mysql');
let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'student'

});

// 尝试连接数据库
sql.connect();

// 接口 request是请求参数的数组  response.send()是向前端返回一段数据
// 得到数据库的数据
express.get('/getstudent', (request, response) => {
        sql.query(`SELECT * FROM student`, (error, data) => {
            if (error) {
                console.log(error)
            } else {
                console.log(data)
                response.send(data)
            }

        })

    })
    // 删除数据
express.get('/cutstudent', (request, response) => {
        sql.query(`DELETE FROM student WHERE id =${request.query.id}`, (error, data) => {
            console.log(request.query)
            if (error) {
                // console.log(error)
            } else {
                // console.log(data)
                response.send('删除成功！！')
            }

        })

    })
    // 添加数据
express.get('/addstudent', (request, response) => {
    sql.query(`	INSERT INTO student (name,age,sex,city,data) VALUES ('${request.query.name}','${request.query.age}','${request.query.sex}','${request.query.city}','${request.query.data}')`, (error, data) => {
        // console.log(request.query)
        // if (error) {
        //     console.log(error)
        // } else {
        //     response.send("注册成功")
        // }

    })

})

express.use(serve(__dirname + '/static'))
express.listen(8080);